home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / levels < prev    next >
Encoding:
Text File  |  2007-04-04  |  7.8 KB  |  284 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '1.1'
  24. __title__ = 'Supply Levels Utility'
  25. __doc__ = "Display bar graphs of current supply levels for supported HPLIP printers."
  26.  
  27. # Std Lib
  28. import sys, getopt, time
  29.  
  30. # Local
  31. from base.g import *
  32. from base import device, status, utils
  33. from prnt import cups
  34.  
  35. DEFAULT_BAR_GRAPH_SIZE = 100
  36.  
  37.  
  38.  
  39. USAGE = [(__doc__, "", "name", True),
  40.          ("Usage: hp-levels [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  41.          utils.USAGE_ARGS,
  42.          utils.USAGE_DEVICE,
  43.          utils.USAGE_PRINTER,
  44.          utils.USAGE_SPACE,
  45.          utils.USAGE_OPTIONS,
  46.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  47.         ("Bar graph size:", "-s<size> or --size=<size> (default=%d)", "option", False),
  48.         ("Use colored bar graphs:", "-c or --color (default is colorized)", "option", False),
  49.         ("Bar graph character:", "-a<char> or --char=<char> (default is '/')", "option", False),
  50.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  51.          utils.USAGE_HELP,
  52.          utils.USAGE_SPACE,
  53.          utils.USAGE_NOTES,
  54.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  55.          ]
  56.  
  57. def usage(typ='text'):
  58.     if typ == 'text':
  59.         utils.log_title(__title__, __version__)
  60.  
  61.     utils.format_text(USAGE, typ, __title__, 'hp-levels', __version__)
  62.     sys.exit(0)
  63.  
  64.  
  65. def logBarGraph(agent_level, agent_type, size=DEFAULT_BAR_GRAPH_SIZE, use_colors=True, bar_char='/'):
  66.     if size == 0: size = 100
  67.     adj = 100.0/size
  68.     if adj==0.0: adj=100.0
  69.     bar = int(agent_level/adj)
  70.     if bar > (size-2): bar = size-2
  71.  
  72.     if use_colors:
  73.         if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM, AGENT_TYPE_CYAN, AGENT_TYPE_CYAN_LOW):
  74.             log.info(utils.codes['teal'])
  75.         elif agent_type in (AGENT_TYPE_MAGENTA, AGENT_TYPE_MAGENTA_LOW):
  76.             log.info(utils.codes['fuscia'])
  77.         elif agent_type in (AGENT_TYPE_YELLOW, AGENT_TYPE_YELLOW_LOW):
  78.             log.info(utils.codes['yellow'])
  79.         elif agent_type == AGENT_TYPE_BLUE:
  80.             log.info(utils.codes['blue'])
  81.         elif agent_type == AGENT_TYPE_BLACK:
  82.             log.info(utils.codes['bold'])
  83.         elif agent_type in (AGENT_TYPE_LG, AGENT_TYPE_G, AGENT_TYPE_PG):
  84.             pass
  85.  
  86.     color = ''
  87.     if use_colors:
  88.         if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM):
  89.             color = utils.codes['fuscia']
  90.  
  91.     log.info(("-"*size)+color)
  92.  
  93.     color = ''
  94.     if use_colors:
  95.         if agent_type in (AGENT_TYPE_CMY, AGENT_TYPE_KCM):
  96.             color = utils.codes['yellow']
  97.  
  98.     log.info("%s%s%s%s (approx. %d%%)%s" % ("|", bar_char*bar, 
  99.              " "*(size-bar-2), "|", agent_level, color))
  100.  
  101.     color = ''
  102.     if use_colors:
  103.         color = utils.codes['reset']
  104.  
  105.     log.info(("-"*size)+color)
  106.  
  107.  
  108.  
  109.  
  110. log.set_module('hp-levels')
  111.  
  112.  
  113. try:
  114.     opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:s:ca:g',
  115.         ['printer=', 'device=', 'help', 'help-rest', 'help-man', 
  116.          'help-desc', 'logging=', 'size=', 'color', 'char='])
  117.  
  118. except getopt.GetoptError:
  119.     usage()
  120.  
  121. printer_name = None
  122. device_uri = None
  123. log_level = logger.DEFAULT_LOG_LEVEL
  124. bus = device.DEFAULT_PROBE_BUS
  125. size = 100
  126. color = True
  127. bar_char = '/'
  128.  
  129. if os.getenv("HPLIP_DEBUG"):
  130.     log.set_level('debug')
  131.  
  132. for o, a in opts:
  133.     if o in ('-h', '--help'):
  134.         usage()
  135.  
  136.     elif o == '--help-rest':
  137.         usage('rest')
  138.  
  139.     elif o == '--help-man':
  140.         usage('man')
  141.  
  142.     elif o == '--help-desc':
  143.         print __doc__,
  144.         sys.exit(0)
  145.  
  146.     elif o in ('-p', '--printer'):
  147.         if a.startswith('*'):
  148.             printer_name = cups.getDefault()
  149.             log.info("Using CUPS default printer: %s" % printer_name)
  150.             log.debug(printer_name)
  151.         else:
  152.             printer_name = a
  153.  
  154.     elif o in ('-d', '--device'):
  155.         device_uri = a
  156.  
  157.     elif o in ('-b', '--bus'):
  158.         bus = a.lower().strip()
  159.         if not device.validateBusList(bus):
  160.             usage()
  161.  
  162.     elif o in ('-l', '--logging'):
  163.         log_level = a.lower().strip()
  164.         if not log.set_level(log_level):
  165.             usage()
  166.  
  167.     elif o in ('-s', '--size'):
  168.         try:
  169.             size = int(a.strip())
  170.         except:
  171.             size = DEFAULT_BAR_GRAPH_SIZE
  172.  
  173.         if size < 0 or size > 200:
  174.             size = DEFAULT_BAR_GRAPH_SIZE
  175.  
  176.     elif o in ('-c', '--color'):
  177.         color = True
  178.  
  179.     elif o in ('-a', '--char'):
  180.         try:
  181.             bar_char = a[0]
  182.         except:
  183.             bar_char = '/'
  184.  
  185.     elif o == '-g':
  186.         log.set_level('debug')
  187.  
  188.  
  189. if device_uri and printer_name:
  190.     log.error("You may not specify both a printer (-p) and a device (-d).")
  191.     usage()
  192.  
  193. utils.log_title(__title__, __version__)
  194.  
  195. if not device_uri and not printer_name:
  196.     try:
  197.         device_uri = device.getInteractiveDeviceURI(bus)
  198.         if device_uri is None:
  199.             sys.exit(1)
  200.     except Error:
  201.         log.error("Error occured during interactive mode. Exiting.")
  202.         sys.exit(1)
  203.  
  204.  
  205. try:
  206.     d = device.Device(device_uri, printer_name)
  207. except Error:
  208.     log.error("Error opening device. Exiting.")
  209.     sys.exit(1)
  210.  
  211. if d.device_uri is None and printer_name:
  212.     log.error("Printer '%s' not found." % printer_name)
  213.     sys.exit(1)
  214.  
  215. if d.device_uri is None and device_uri:
  216.     log.error("Malformed/invalid device-uri: %s" % device_uri)
  217.     sys.exit(1)
  218.  
  219. user_cfg.last_used.device_uri = d.device_uri
  220.  
  221. try:
  222.     d.open()
  223.     d.queryDevice()
  224. except Error, e:
  225.     log.error("Error opening device (%s). Exiting." % e.msg)
  226.     sys.exit(1)
  227.  
  228. if d.mq['status-type'] != STATUS_TYPE_NONE:
  229.  
  230.     log.info("")
  231.  
  232.     sorted_supplies = []
  233.     a = 1
  234.     while True:
  235.         try:
  236.             agent_type = int(d.dq['agent%d-type' % a])
  237.             agent_kind = int(d.dq['agent%d-kind' % a])
  238.         except KeyError:
  239.             break
  240.         else:
  241.             sorted_supplies.append((a, agent_kind, agent_type))
  242.  
  243.         a += 1
  244.  
  245.     sorted_supplies.sort(lambda x, y: cmp(x[2], y[2]) or cmp(x[1], y[1]))
  246.  
  247.     for x in sorted_supplies:
  248.         a, agent_kind, agent_type = x
  249.         agent_health = d.dq['agent%d-health' % a]
  250.         agent_level = d.dq['agent%d-level' % a]
  251.         agent_sku = str(d.dq['agent%d-sku' % a])
  252.         agent_desc = d.dq['agent%d-desc' % a]
  253.         agent_health_desc = d.dq['agent%d-health-desc' % a]
  254.  
  255.         if agent_health == AGENT_HEALTH_OK and \
  256.             agent_kind in (AGENT_KIND_SUPPLY,
  257.                             AGENT_KIND_HEAD_AND_SUPPLY,
  258.                             AGENT_KIND_TONER_CARTRIDGE,
  259.                             AGENT_KIND_MAINT_KIT,
  260.                             AGENT_KIND_ADF_KIT,
  261.                             AGENT_KIND_INT_BATTERY,
  262.                             AGENT_KIND_DRUM_KIT,):
  263.  
  264.             log.info(utils.bold(agent_desc))
  265.             log.info("Part No.: %s" % agent_sku)
  266.             log.info("Health: %s" % agent_health_desc)
  267.             logBarGraph(agent_level, agent_type, size, color, bar_char)
  268.             log.info("")
  269.  
  270.         else:
  271.             log.info(utils.bold(agent_desc))
  272.             log.info("Part No.: %s" % agent_sku)
  273.             log.info("Health: %s" % agent_health_desc)
  274.             log.info("")
  275.  
  276.  
  277. else:
  278.     log.error("Status not supported for selected device.")
  279.     sys.exit(1)
  280.  
  281. d.close()
  282. sys.exit(0)
  283.  
  284.